package com.inossem.warehousestorage_inossem.activity.allocation.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.PackageDetailActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInPackageAdapter;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInSnAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationGetSNsByPackageBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNItemInfo;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationGetSNsByPackageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;
import com.inossem.warehousestorage_inossem.utils.NoScrollListView;

import java.io.Serializable;
import java.util.List;

/**
 * 序列号列表
 *
 * @author 詹建宇
 * @time on 2018/4/4 14:53
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInSNsActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private NoScrollListView packageListView, snListView;

    private String mat_id;
    private String position_code;

    private String position_id, area_id, wh_id;

    private AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean;

    //该物料下 对应仓位下的 已确认的 序列号列表
    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list;
    //该序列号列表下 不重复的 打包码列表
    private List<String> codes;
    //该序列号列表下 不属于任何打包码下的 序列号集合
    private List<String> snList;

    private AllocationInPackageAdapter packageAdapter;
    private AllocationInSnAdapter snAdapter;

    private EditText conditionEdit;
    private ImageView searchImg;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_allocation_sn_listview);

        initView(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        initData(this);
        initOper(this);
    }

    private void getExtra(Activity activity) {
        mat_id = activity.getIntent().getStringExtra(AllocationInStorageExtra.MAT_ID);
        position_code = activity.getIntent().getStringExtra(AllocationInStorageExtra.POSITION_CODE);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        packageListView = (NoScrollListView) activity.findViewById(R.id.packageListView);
        snListView = (NoScrollListView) activity.findViewById(R.id.snListView);

        conditionEdit = (EditText) activity.findViewById(R.id.conditionEdit);
        searchImg = (ImageView) activity.findViewById(R.id.searchImg);
    }

    @Override
    public void initData(Activity activity) {
        getExtra(activity);
        title.setText(position_code);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        itemListBean = AllocationUtils.getItemListBeanByMatId(activity, mat_id);
        list = AllocationUtils.getSNList(activity, mat_id, position_code);
        codes = AllocationUtils.getPackageCodes(list);
        snList = AllocationUtils.getSNListNoPackage(list);

        position_id = list.get(0).getPosition_id();
        area_id = list.get(0).getArea_id();
        wh_id = list.get(0).getWh_id();

        addPackageListData();
        addSNListData();

        setmScanListener((String scanResult, int length) -> {
            String condition = null;
            switch (QRCodeUtils.getQRCodeType(scanResult)) {
                case QRCodeConstant.CY_TYPE_PACKAGE:
                case QRCodeConstant.CY_TYPE_SN:
                    condition = scanResult;
                    break;
                default:
                    break;
            }

            if (!TextUtils.isEmpty(condition)) {
                searchSN(this, condition);
            }

        }, QRCodeConstant.CY_TYPE_PACKAGE, QRCodeConstant.CY_TYPE_SN);
    }

    private void addPackageListData() {
        packageAdapter = new AllocationInPackageAdapter(this, codes);
        packageListView.setAdapter(packageAdapter);
    }

    private void addSNListData() {
        snAdapter = new AllocationInSnAdapter(this, snList);
        snListView.setAdapter(snAdapter);
    }

    private void initOper(Activity activity) {
        packageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //进入序列号列表页
                List<SNItemInfo> snList = AllocationUtils.getSNsByPackage(list, codes.get(i));
                Intent intent = new Intent(activity, PackageDetailActivity.class);
                intent.putExtra(OutStorageExtra.SN_LIST, (Serializable) snList);
                startActivity(intent);
            }
        });

        packageListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                //删除
                DialogUtils.getConfirmDialog(activity, "确认删除?", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        AllocationUtils.deletePackage(activity, mat_id, codes.get(i));
                        codes.remove(i);
                        packageAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                }).show();
                return true;
            }
        });

        snListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                //删除
                DialogUtils.getConfirmDialog(activity, "确认删除?", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        AllocationUtils.deleteSN(activity, mat_id, snList.get(i));
                        snList.remove(i);
                        snAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                }).show();
                return true;
            }
        });

        searchImg.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                searchSN(activity, conditionEdit.getText().toString().trim());
            }
        });
    }

    private void searchSN(Activity activity, String condition) {
        AllocationGetSNsByPackageRequest.request(activity, condition, itemListBean.getLocation_id(), new GsonBeanListener<AllocationGetSNsByPackageBean>() {
            @Override
            public void onSuccess(AllocationGetSNsByPackageBean bean) {
                AllocationUtils.setSNsInPosition(activity, mat_id, bean, position_code, position_id, area_id, wh_id);
                initData(activity);
            }
        });
    }

}
